var functions = {
	
	currentPage : "home.php",
	classId : 3,
	tournamentId : 1,
	
	getPageToLoad : function(menuElement){
		link = menuElement.data("link");
		if (this.isUndefined(link))
			link = menuElement.closest(".menuList").find(".menuTitle").data("link");

		return link;
	},

	loadSubPage : function(params){
		params = this.setDefaultParameters(params);
		this.makeAjaxCall(params);
	},

	getParameters : function(menuElement){
		params = menuElement.data("parameters");

		if (functions.isUndefined(params))
			return params;

		params = functions.returnsArrayIfEmpty(params.indexOf("&"), params);

		parameters = functions.generateParametersObject(params);

		return parameters;
	},
	
	generateParametersObject: function(params){
		parameters = {};

		$.each(params, function(){
			parameter = this.split("=");
			parameterSelected = "parameters." + parameter[0] + " = '" + parameter[1] + "'";
			eval(parameterSelected);
		});

		return parameters;
	},
	
	returnsArrayIfEmpty : function(index, params){
		if (index == -1){ 
			//Means that there is just one parameter;
			paramsArray = [];
			paramsArray.push(params);
			params = paramsArray;
		}

		return params;
	},

	makeAjaxCall : function(params){
		//$.blockUI({message : '<div class="wait">Cargando...</div>'});
		ajaxCall = 
			{
				url : params.pageToLoad,
				data : params.parameters,
				type : params.HTTPMethod,
				dataType : params.receivedType, 
				success : function(receivedData){
					if (params.callbackFunction != undefined){
						params.callbackFunction(receivedData);
					}else{
						functions.setCurrentState(params);
						$("#mainContent").html(receivedData);
					}
				},
				complete : function(){
					if (params.completeFunction != undefined){
						params.completeFunction();
					}
					//$.unblockUI();
				}
			};
			$.ajax(ajaxCall);	
	},

	setCurrentState : function(params){
		params.parameters = {
			classId : this.getParamFromString(params, "class_id") || 0,
			tournamentId : this.getParamFromString(params, "tournament_id") || 0
		};

		this.currentPage = params.pageToLoad;
		this.classId = params.parameters.classId;
		this.tournamentId = params.parameters.tournamentId;
	},
	
	getParamFromString : function(params, which){
		if (typeof params.parameters === "string"){
			arrParams = params.parameters.split("&");
			amountParams = arrParams.length;
			for(a = 0; a < amountParams; a++){
				parameter = arrParams[a].split("=");
				if (parameter[0] == which)
					return parseInt(parameter[1]);
			}
			return 0;
		}else{
			return params.which;
		}
	},

	setDefaultParameters : function(params){
		if (params === undefined)
			return {};

		if (params.HTTPMethod === undefined || params.HTTPMethod.length == 0)
			params.HTTPMethod = "post";

		if (params.receivedType === undefined || params.receivedType.length == 0)
			params.receivedType = "html";

		if (params.parameters === undefined || params.parameters.length == 0)
			params.parameters = {
				tournamentId : this.tournamentId,
				classId : this.classId
			};

		if (params.pageToLoad === undefined || params.pageToLoad.length == 0)
			params.pageToLoad = "home.php";

		return params;
	},

	trim : function(myString){
		return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
	},

	isUndefined : function(element){
		return element === undefined;
	},
	
	validateAllCompleted : function (){
		$.each($('.requiredData'), function(){
			if ($(this).hasClass('emptyText')){
				$(this).addClass('error');
				$('.requiredData.error:first').focus();
			}
		});
	},
	
	errorExists : function(){
		return $('.error').length > 0;
	},
	
	setEmptyText : function(element, defaultText){
		if ($(element).val() == defaultText){
			$(element).addClass('emptyText');
			$(element).val('');
		}
	}

};